package com.sheng.controller;

import com.sheng.pojo.Book;
import com.sheng.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
public class BookController {

    @GetMapping("/")
    public String index(Model model){
        model.addAttribute("tip","欢迎访问第一个SpringBoot应用");
        return "hello";
    }

    @GetMapping("/rest")
    @ResponseBody
    public ResponseEntity restIndex(){
        return new ResponseEntity<>("欢迎访问第一个SpringBoot应用",null, HttpStatus.OK);
    }

    @Autowired
    @Qualifier("bookServiceImpl")
    private BookService bookService;

    @PostMapping("/addBook")
    public String addBook(Book book, Model model){
        bookService.addBook(book);
        return "redirect:listBooks";
    }

    @PostMapping("/rest/books")
    @ResponseBody
    public ResponseEntity<Map<String,String>> restAddBook(@RequestBody Book book){
        bookService.addBook(book);
        return new ResponseEntity<>(Map.of("tip","添加成功"),null,HttpStatus.OK);
    }

    @GetMapping("listBooks")
    public String list(Model model){
        model.addAttribute("books",bookService.getAllBooks());
        return "list";
    }

    @GetMapping("/rest/books")
    @ResponseBody
    public  ResponseEntity<List<Book>> restList(){
        return new ResponseEntity<>((List<Book>)bookService.getAllBooks(), null, HttpStatus.OK);
    }

    @GetMapping("deleteBook")
    public String delete(Long id){
        bookService.deleteBook(id);
        return "redirect:listBooks";
    }

    @DeleteMapping("/rest/books/{id}")
    @ResponseBody
    public ResponseEntity<Map<String,String>> restDelete(@PathVariable Long id){
        bookService.deleteBook(id);
        return new ResponseEntity<>(Map.of("tip","删除成功"), null, HttpStatus.OK);
    }
}
